import React from 'react';
import {TreeSelect} from "antd";
import {TreeSelectProps} from "antd/lib/tree-select";
import {convertDicValueByType, listToTree} from "@/utils/utils";
import {DicValueType} from "@/global";
import useDicListByCategoryCode from "@/hooks/useDicListByCategoryCode";
import {SysDicModel} from "@/pages/sys/dic/data";

export interface DicTreeSelectProps extends TreeSelectProps<{}> {
  /** 字典编码 */
  dicCode: string;
  /** 字典值类型 */
  dicValueType?: DicValueType;
  /** 父主键 */
  dataParentId?: string;
}

/**
 * 字典下拉树
 */
const DicTreeSelect = function (props: DicTreeSelectProps) {
  const {dicCode, dicValueType = DicValueType.string, dataParentId, ...rest} = props;
  const dicList = useDicListByCategoryCode(dicCode);
  const treeList = listToTree(dicList, (p: SysDicModel) => {
    p.title = p.name;
    p.value = convertDicValueByType(p.value, dicValueType);
    p.icon = (<i className="icon-notebook"/>);
  });

  return (
    <TreeSelect
      {...rest}
      treeData={treeList}
    />
  );
};
/** 组件默认值 */
DicTreeSelect.defaultProps = {
  treeIcon: true,
};
export default React.memo(DicTreeSelect);
